/*
 * Copyright 2007, 2008 Tiberiumlabs
 *
 * This file is part of Tiberiumlabs Lailaps.
 *
 * Tiberiumlabs Lailaps is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Tiberiumlabs Lailaps is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.tiberiumlabs.lailaps.gui.table;

import javax.swing.DefaultListSelectionModel;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.TableColumn;
import org.tiberiumlabs.lailaps.Mediator;

/**
 *
 * @author <a href="paranoid.tiberiumlabs@gmail.com">Paranoid</a>
 */
public class TableColumnsModel extends DefaultTableColumnModel implements ColumnsNames {

    protected static String[] columnsNames = {"S", "Link", "File", "Path", "Size", "Completed", "Speed",
                                     "Progress", "Percents", "Time left", "Time Spent",
                                     "Segments", "Segments progress"
    };

    public TableColumnsModel() {
        super();
        createColumns();
    }

    @Override
    protected ListSelectionModel createSelectionModel() {
        ListSelectionModel sm = new DefaultListSelectionModel();
        sm.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        return super.createSelectionModel();
    }

    private void createColumns() {
        int[] columnsOrder = Mediator.getMediator().getSettings().getInternalInterfaceSettings().getTableColumnsOrder();

        // checking for empty settings
        if (columnsOrder == null || columnsOrder.length == 0) {
            // create default column set
            addColumn(createColumn(COL_STATUS));
            addColumn(createColumn(COL_LINK));
            addColumn(createColumn(COL_FILE));
            addColumn(createColumn(COL_COMPLETED));
            addColumn(createColumn(COL_SIZE));
            return;
        }

        for (int i = 0; i < columnsOrder.length; i++) {
            addColumn(createColumn(columnsOrder[i], columnsOrder[++i]));
        }
    }

    private TableColumn createColumn(int index) {
        TableColumn col = new TableColumn(index);
        col.setHeaderValue(columnsNames[index]);
        return col;
    }

    private TableColumn createColumn(int index, int width) {
        TableColumn col = createColumn(index);
        col.setWidth(width);
        return col;
    }

    public void addColumn(int index) {
        if (columnInVector(index) == null) {
            addColumn(createColumn(index));
        }
    }

    public void removeColumn(int index) {
        TableColumn col = columnInVector(index);
        if (col != null) {
            removeColumn(col);
            checkForLastColumn();
        }
    }

    private TableColumn columnInVector(int index) {
        for (int i = 0; i < tableColumns.size(); i++) {
            TableColumn tableColumn = tableColumns.get(i);
            if (tableColumn.getModelIndex() == index) {
                return tableColumn;
            }
        }
        return null;
    }

    private void checkForLastColumn() {
        if (getColumnCount() < 1) {
            addColumn(createColumn(COL_LINK));
        }
    }

    public int[] getColumnsOrder() {
        int tableColumnsSize = tableColumns.size();
        int[] result = new int[tableColumnsSize*2];
        int arrayPosition = 0;
        for (int i = 0; i < tableColumnsSize; i++) {
            TableColumn col = tableColumns.get(i);
            result[arrayPosition] = col.getModelIndex();
            arrayPosition++;
            result[arrayPosition] = col.getWidth();
            arrayPosition++;
        }
        return result;
    }

}
